package com.skyvin.nreceipts.user.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skyvin.nreceipts.admin.service.ReceiptCategoryService;
import com.skyvin.nreceipts.admin.service.ReceiptItemCategoryService;
import com.skyvin.nreceipts.admin.service.RetailerService;
import com.skyvin.nreceipts.common.document.AppRole;
import com.skyvin.nreceipts.common.document.AppUser;
import com.skyvin.nreceipts.security.value.UserValue;
import com.skyvin.nreceipts.user.mongorepository.AppRoleRepository;
import com.skyvin.nreceipts.user.mongorepository.AppUserRepository;
import com.skyvin.nreceipts.user.util.UserRole;

@Service
public class UserService {
	Logger logger = Logger.getLogger(UserService.class);
	
	@Autowired
	AppUserRepository appUserRepository;
	
	@Autowired
	AppRoleRepository appRoleRepository;
	
	@Autowired
	ReceiptCategoryService receiptCategoryService;
	
	@Autowired
	ReceiptItemCategoryService receiptItemCategoryService;
	
	@Autowired
	RetailerService retailerService;

	@Transactional
	public AppUser signup(AppUser appUser) {
		logger.info("user role : " + UserRole.USER.getRoleName());
		AppRole appRole = appRoleRepository.findByName(UserRole.USER.getRoleName());
		appRole.setName(UserRole.USER.getRoleName());
		appUser.setAppRole(appRole);
		
		AppUser appUserUpdated = appUserRepository.save(appUser);
		
		// Copying from receiptcategory template to user specific receipt category
		receiptCategoryService.clonefromReceiptCategoryTemplate(appUserUpdated);
		
		// Copying from receiptcategory template to user specific receipt category
		receiptItemCategoryService.clonefromReceiptItemCategoryTemplate(appUserUpdated);
		
		// Copying from retailer template to user specific retailer category
		retailerService.clonefromRetailerTemplate(appUserUpdated);

		return appUserUpdated;
	}
	

	@Transactional
	public AppUser save(AppUser appUserDoc) {
		AppRole appRole = appUserRepository.findOne(appUserDoc.getId()).getAppRole();
		if(appRole != null) {
			appUserDoc.setAppRole(appRole);
			logger.info("user role of the user to be saved : " + appRole.getName());
		}
		appUserRepository.save(appUserDoc);
		return appUserDoc;
	}
	
	@Transactional
	public void removeUserFromSystem(String userPID) {
		appUserRepository.delete(userPID);
		
	}
	
	public AppUser getCurrentAppUser() {
		AppUser appUser = appUserRepository.findOne(getCurrentUserPID());
		return appUser;
	}
	public UserValue getCurrentUserValue() {
		UserValue userValue = (UserValue) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return userValue;
	}
	public String getCurrentUserPID() {
		UserValue userValue = (UserValue) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return userValue.getId();
	}
	
	public AppUser getUserByUserID(String userId) {
		return appUserRepository.findByUserId(userId);
	}
}